<template>
    <div>
        <div class="bg-banner" />
        <div id="login-box">
            <div class="login-banner" />
            <el-form ref="form" :model="form" :rules="rules" class="login-form" auto-complete="on" label-position="left">
                <div class="title-container">
                    <h3 class="title">{{ title }}管理后台</h3>
                </div>
                <div>
                    <el-form-item prop="username">
                        <el-input ref="name" v-model="form.username" placeholder="用户名" type="text" tabindex="1" auto-complete="off">
                            <svg-icon slot="prefix" name="user" />
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input ref="password" v-model="form.password" :type="passwordType" placeholder="密码" tabindex="2" auto-complete="off" @keyup.enter.native="handleLogin">
                            <svg-icon slot="prefix" name="password" />
                            <svg-icon slot="suffix" :name="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPassword" />
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="code" class="code-container">
                        <el-input ref="code" v-model="form.code" auto-complete="off" placeholder="验证码" style="width: 230px;" maxlength="3" @keyup.enter.native="handleLogin">
                            <svg-icon slot="prefix" name="user" />
                        </el-input>
                        <div class="login-code">
                            <img :src="codeUrl" class="login-code-img" style="width: 100%; cursor: pointer;" @click="getCode">
                        </div>
                    </el-form-item>
                </div>
                <el-checkbox v-model="form.remember">记住我 </el-checkbox>
                <el-button :loading="loading" type="primary" style="width: 100%;" @click.native.prevent="handleLogin">登 录</el-button>
            </el-form>
        </div>
        <Copyright v-if="$store.state.settings.showCopyright" />
    </div>
</template>

<script>
import { loginMethod, getCodeImg } from '@/api/login'
export default {
    name: 'Login',
    data() {
        return {
            title: process.env.VUE_APP_TITLE,
            form: {
                username: localStorage.login_account || '',
                password: '',
                code: '',
                remember: !!localStorage.login_account,
                uuid: ''
            },
            rules: {
                username: [
                    { required: true, trigger: 'blur', message: '请输入用户名' }
                ],
                password: [
                    { required: true, trigger: 'blur', message: '请输入密码' },
                    { min: 5, max: 18, trigger: 'blur', message: '密码长度为5到18位' }
                ],
                code: [
                    { required: true, trigger: 'blur', message: '请输入验证码' }
                ]
            },
            loading: false,
            passwordType: 'password',
            redirect: undefined,
            codeUrl: ''
        }
    },
    watch: {
        $route: {
            handler: function(route) {
                console.log(route)
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    created() {
        this.getCode()
    },
    methods: {
        getCode() {
            getCodeImg().then(res => {
                this.codeUrl = 'data:image/gif;base64,' + res.img
                this.form.uuid = res.uuid
            })
        },
        showPassword() {
            this.passwordType = this.passwordType === 'password' ? '' : 'password'
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        handleLogin() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true

                    const { username, password, code, uuid } = this.form
                    loginMethod({username, password, code, uuid}).then(res => {
                        if (res.code == 200) {
                            let failure_time = Date.parse(new Date()) + 24 * 60 * 60 * 30
                            let token = res.token
                            this.$message.success('登录成功')
                            localStorage.setItem('username', username)
                            localStorage.setItem('token', token)
                            localStorage.setItem('failure_time', failure_time) // 一个月有效时间
                            this.form.remember && localStorage.setItem('login_account', this.form.username)
                            this.$store.dispatch('user/setLoginInfo', {username, token, failure_time}).then(() => {
                                //this.$router.push({ path: '/welcome' })
                                this.$router.push({ path: '/business/house_property' })
                            })
                        } else {
                            this.getCode()
                            this.form.code = ''
                            this.$refs.code.focus()
                        }
                        this.loading = false
                    }).catch(() => {
                        this.getCode()
                        this.$refs.code.focus()
                        this.form.code = ''
                        this.loading = false
                    })

                    /* this.$store.dispatch('user/login', this.form).then(() => {
                        this.loading = false
                        this.form.remember && localStorage.setItem('login_account', this.form.account)
                        this.$router.push({ path: this.redirect || '/' })
                    }).catch(() => {
                        this.loading = false
                    }) */
                }
            })
        },
        testAccount(account) {
            this.form.account = account
            this.form.password = '123456'
            this.handleLogin()
        }
    }
}
</script>

<style lang="scss" scoped>
[data-mode=mobile] {
    #login-box {
        max-width: 80%;
        .login-banner {
            display: none;
        }
    }
}
::v-deep input[type=password]::-ms-reveal {
    display: none;
}
.bg-banner {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    background-image: url(../assets/images/login-bg.jpg);
    background-size: cover;
    background-position: center center;
}
#login-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 0.8);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 5px #999;
    .login-banner {
        width: 300px;
        background-image: url(../assets/images/login-banner.jpg);
        background-size: cover;
        background-position: center center;
    }
    .login-form {
        width: 450px;
        padding: 50px 35px 30px;
        overflow: hidden;
        .svg-icon {
            vertical-align: -0.35em;
        }
        .title-container {
            position: relative;
            .title {
                font-size: 22px;
                color: #666;
                margin: 0 auto 30px;
                text-align: center;
                font-weight: bold;
                @include text-overflow;
            }
        }
    }
    ::v-deep .el-input {
        display: inline-block;
        height: 48px;
        width: 100%;
        input {
            height: 48px;
        }
        .el-input__prefix {
            left: 10px;
        }
        .el-input__suffix {
            right: 10px;
        }
    }
    .el-checkbox {
        margin: 20px 0;
    }
}
.copyright {
    position: absolute;
    bottom: 30px;
    width: 100%;
    margin: 0;
    mix-blend-mode: difference;
}
::v-deep .code-container div {
    display: flex;
}
.login-code {
    width: 130px;
    display: inline-block;
    border: 1px solid #eee;
    margin-left: 10px;
    border-radius: 5px;
    background-color: #fff;
    height: 48px;
}
</style>
